using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using productsMarket;

namespace elementManagement
{
    public static class ElementOperations
    {

        private static decimal EFFICIENCYBORDER = 0.01M;

        public static bool canCompose(List<Element> inventory, List<Element> elementGuidList)
        {
            decimal resultEfficiency = 1;
            foreach (Element elem in elementGuidList) 
            {
                Parameters p = elem.parameters;
                if (p == null)
                {
                    p = elem.computeParameters();
                }
                if (p == null)
                {
                    resultEfficiency = 1;
                }
                else
                {
                    resultEfficiency *= p.efficiency;
                }    
            }
            if (resultEfficiency > EFFICIENCYBORDER)
            {
                return true;
            }
            return false;
        }

        public static void compose(List<Element> inventory, List<Element> elementGuidList, string Nume)
        {
            if (canCompose(inventory, elementGuidList)){
                Element compoundElement = new Element();
                compoundElement.name = Nume;
                compoundElement.parameters = null; // make sure it stays a compound element and not an atom
                foreach (Element elem in inventory) 
                {
                    if (containsElement(elementGuidList, elem)) 
                    {
                        compoundElement.content.Add(elem);
                    }
                }
                foreach (Element elem in compoundElement.content)
                {
                    inventory.Remove(elem);
                }                
                inventory.Add(compoundElement);
            }
        }

        private static bool containsElement(List<Element> elements, Element element)
        {
            foreach (Element elem in elements)
            {
                if (elem.UniqueID.Equals(element.UniqueID))
                {
                    return true;
                }
            }
            return false;
        }

        public static void decompose(List<Element> inventory, Element element)
        {
            Parameters parameters = element.computeParameters();
            if (parameters.health == 0) 
            {
                inventory.Remove(element);
            }
            if (parameters.efficiency == 0) {
                //decompose element
                inventory.Remove(element);
                foreach (Element component in element.content) {
                    if (component.content == null)
                    {
                        // it's an atom
                        if (component.computeParameters().efficiency != 0) {
                            // healthy aoms get back in
                            inventory.Add(component);
                        }
                    }
                    else {
                        // it's a compound element
                        if (component.computeParameters().efficiency == 0)
                        {
                            decompose(inventory, component);
                        }
                        else
                        {
                            // healthy compund elements get back in
                            inventory.Add(component);
                        }
                    }
                }
            }
        }
    }
}
